package com.sz.biz.common.project.service.impl;

import com.sz.biz.common.constants.ErrorCodes;
import com.sz.biz.logistics.constants.ProjectConstants;
import com.sz.biz.trade.constants.TrdErrorCodes;
import com.sz.biz.common.project.entity.Project;
import com.sz.biz.common.project.entity.ProjectFile;
import com.sz.biz.common.project.service.ProjectFileService;
import com.sz.biz.common.project.service.ProjectService;
import com.sz.biz.common.project.dto.ProjectFileDto;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.utils.StringUtils;
import com.sz.common.core.system.entity.SysFile;
import com.sz.common.core.system.service.SysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Function: TODO: ADD FUNCTION <br>
 * Author: qiankun<br>
 * Date: 2017-07-13 11:17:00
 */
@Service("ProjectFileService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ProjectFileServiceImpl extends AbstractService implements ProjectFileService {
    @Autowired
    private SysFileService sysFileService;

    @Autowired
    private ProjectService projectService;

    @Override
    public void save(ProjectFile file) {
        checkData(file);
        dao.save("ProjectFileMapper.insertSelective", file);
    }

    private void checkData(ProjectFile file) {
        if (file != null) {
            if (StringUtils.isEmpty(file.getFileId())) {
                throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "fileId");
            }
            if (StringUtils.isEmpty(file.getProjectId())) {
                throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "projectId");
            }
            if (StringUtils.isEmpty(file.getType()) || file.getType() == 0) {
                throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "type");
            }
        }
    }

    @Override
    public void update(ProjectFile file) {
        if (file != null) {
            if (StringUtils.isEmpty(file.getProjectId())) {
                throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "projectId");
            }
            dao.update("ProjectFileMapper.update", file);
        }
    }

    @Override
    public void delete(int id) {
        if (id == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "id");
        }

        dao.delete("ProjectFileMapper.deleteByPrimaryKey", id);
    }

    @Override
    public void deleteByProjectId(Integer projectId, Integer type) {
        if (StringUtils.isEmpty(projectId) || projectId == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "projectId");
        }
        if (StringUtils.isEmpty(type) || type == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "type");
        }
        Project project = projectService.findBaseById(projectId);
        if (project.getStatus() >= ProjectConstants.Status.APPROVED) {
            throw Exceptions.bizException(TrdErrorCodes.ERROR_PROJECT_CAN_NOT_DELETE_FILE);
        }
        Map<String,Object> param= new HashMap<>();
        param.put("projectId",projectId);
        param.put("type",type);
        dao.delete("ProjectFileMapper.deleteByProjectIdAndType",param);
    }

    @Override
    public List<ProjectFile> queryByProjectId(int projectId, int type) {
        if (projectId == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "projectId");
        }
        if (type == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "type");
        }
        Map<String, Object> param = new HashMap<>();
        param.put("projectId", projectId);
        param.put("type", type);
        return dao.findForList("ProjectFileMapper.queryByProjectIdAndType", param, ProjectFile.class);
    }

    @Override
    public void saveFiles(String fileIds, int id, int projectId) {
        if (StringUtils.isEmpty(projectId) || projectId == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "projectId");
        }
        if (StringUtils.isEmpty(id) || id == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "id");
        }
        Map<String, Object> pd = new HashMap<>();
        pd.put("fileId", fileIds);
        pd.put("id", id);
        pd.put("projectId", projectId);
        dao.update("ProjectFileMapper.updateFiles", pd);
    }

    @Override
    public List<ProjectFileDto> queryByProjectDetail(int projectId, int type) {
        List<ProjectFile> lists = queryByProjectId(projectId, type);
        List<ProjectFileDto> projectFileDtoList = new ArrayList<>();
        lists.forEach(projectFile -> {
            List<SysFile> fileInfos = sysFileService.getFileInfos(getListFiles(projectFile.getFileId()));
            for (SysFile file : fileInfos) {
                ProjectFileDto projectFileDto = new ProjectFileDto();
                projectFileDto.setName(projectFile.getName());
                projectFileDto.setRemark(projectFile.getRemark());
                projectFileDto.setFileName(file.getFileName());
                projectFileDto.setFilePath(file.getFileLocation());
                projectFileDto.setFileId(projectFile.getFileId());
                projectFileDto.setContentType(file.getContentType());
                projectFileDto.setId(projectFile.getId());
                projectFileDtoList.add(projectFileDto);
            }
        });
        return projectFileDtoList;
    }

    @Override
    public void deleteByIdProjectIdAndType(int projectId, int id, int type) {
        if (projectId == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "projectId");
        }
        if (id == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "id");
        }
        if (type == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "type");
        }
        Map<String, Object> param = new HashMap<>();
        param.put("projectId", projectId);
        param.put("type", type);
        param.put("id", id);
        dao.delete("ProjectFileMapper.deleteByIdProjectIdAndType",param);
    }

    private List<String> getListFiles(String fileId){
        List<String> list = new ArrayList<>();
        if(!StringUtils.isEmpty(fileId)){
            String [] files = fileId.split(",");
            list = Arrays.asList(files);
        }
        return list;
    }
}
